package club.kingyin.kyblog;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 博客启动类程序
 *
 * @author kingyin
 * @version 1.0
 * @website 正在建设
 * @date 2021/8/12 14:40
 * @since 1.0

 */
@MapperScan("club.kingyin.kyblog.core.business.mapper")
@SpringBootApplication()
@EnableTransactionManagement
public class KyBlogWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(KyBlogWebApplication.class, args);
    }

    /**
     * 配置同源策略
     * @return 同源策略
     */
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }
    /**
     * 跨域过滤器
     * @return corsFilter
     */
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); // 4
        return new CorsFilter(source);
    }
}
